<?php
/**
 * Created by PhpStorm.
 * User: 式神 (luck48.com)
 * Email: 289650682@qq.com
 * Name: ${NAME}Administrator
 * Date: 2017-04-11
 * Time: 16:39
 */
namespace App\Http\Controllers\Admin;

use App\Http\Models\Config;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ConfigController extends CommonController
{
    //推荐位列表
    public function configlist(Request $request){
        if($request->isMethod('post')){
            $pagenum=config('system.page_num');
            $p=Input::get('p')?Input::get('p'):1;
            $json=[];
            $model=new Config();
            $count=$model->count();
            $list=$model->skip(($p-1)*$pagenum)->take($pagenum)->orderBy('conf_id','asc')->get()->toArray();

            $data['list']=$list;
            $data['title']=trans('config.listtitle');
            $json['status']=200;
            $json['pages']=ceil($count/$pagenum);
            $json['data']=$data;
            return $json;
        }
        return view('admin.config.list');
    }

    public function configAdd(Request $request){
        if($request->isMethod('post')){
            $input = Input::except('_token');
            $rules = [
                'conf_name'=>'required',
                'conf_title'=>'required',
            ];

            $message = [
                'conf_name.required'=>'配置项名称不能为空！',
                'conf_title.required'=>'配置项标题不能为空！',
            ];

            $validator = Validator::make($input,$rules,$message);

            if($validator->passes()){
                $re = Config::create($input);
                if($re){
                    $data['status']=200;
                    $data['msg']=trans('config.submit_success');
                    $data['url']='reload';
                }else{
                    $data['status']=300;
                    $data['msg']=trans('config.submit_error');
                }
            }else{
                $data['status']=300;
                $data['msg']=$validator->messages()->first();
            }
            return $data;
        }
        return view('admin.config.add');
    }

    public function configEdit(Request $request){
        if($request->isMethod('post')){
            $input = Input::except('_token','_method','conf_id');
            $re = Config::where('conf_id',Input::get('conf_id'))->update($input);
            if($re){
                $this->putFile();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                $data['url']='reload';
            }else{
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
        $data = Config::find(Input::get('id'));
        return view('admin.config.edit',compact('data'));
    }

    //删除
    public function configDel(Request $request){
        if($request->isMethod('post')){
            $id=Input::get('id');
            $id=explode(",", $id);
            $m=new Config();
            DB::beginTransaction();
            try{
                $m->whereIn('id',$id)->delete();
                DB::commit();
                $this->putFile();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                $data['url']='reload';
            }catch (\Exception $e) {
                DB::rollBack();
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
    }

    public function welcome(Request $request){
        if($request->isMethod("post")){
            $input = Input::all();
            foreach($input['conf_id'] as $k=>$v){
                Config::where('conf_id',$v)->update(['conf_content'=>$input['conf_content'][$k]]);
            }
            $this->putFile();
            $data['status']=200;
            $data['msg']=trans('config.submit_success');
            return $data;
        }
        $data=Config::whereIn('conf_id',[1,2])->get();
        return view('admin.config.welcome',compact('data'));
    }

    public function putFile()
    {
        $config = Config::pluck('conf_content','conf_name')->all();
        $path = base_path().'\config\web.php';
        $str = '<?php return '.var_export($config,true).';';
        file_put_contents($path,$str);
    }
}